In [1]:
%%javascript
//hack to fix export
require.config({
  paths: {
    d3: 'https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3',
    jquery: 'https://code.jquery.com/jquery-3.4.1.min',
    plotly: 'https://cdn.plot.ly/plotly-latest.min'
  },

  shim: {
    plotly: {
      deps: ['d3', 'jquery'],
      exports: 'plotly'
    }
  }
});
In [2]:
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import math
In [3]:
df = pd.read_csv("database.csv")
df["y"] = 1 - df["y"]
df["time"] = (df["time"] - min(df["time"])).astype(int)
In [4]:
df_grouped = df.groupby("id")
df["oposite"] = df["y"] - df_grouped["y"].shift(1)
df["adjacent"] = df["x"] - df_grouped["x"].shift(1)
df["distance"] = (df["adjacent"] ** 2 + df["oposite"] ** 2) ** (1. / 2.)
moves = df[df["distance"] > 0].reset_index()
In [5]:
# DESPLAÇAMENTS

fig = go.Figure()
fig.add_trace(go.Scatter(
        name = "desplaçaments",
        mode = "lines",
        x = df["x"],
        y = df["y"],
        line = dict(
            color = "#333333"
        )
    )
)

fig.update_yaxes(
    visible = False
)

fig.update_xaxes(
    visible = False
)

fig.update_layout(
    height = 600,
    width = 800,
    title_text = "Desplaçaments sobre l'escenari",
    paper_bgcolor = "rgba(256, 256, 256, 256)",
    plot_bgcolor = "rgba(230, 230, 230, 256)"
)
In [6]:
# DESPLAÇAMENTS PER PESONATGES

fig = make_subplots(rows = 5, cols = 2)
for n, player in enumerate(pd.unique(df["player"])):
    fig.add_trace(
        go.Scatter(
            name = player,
            x = df[df["player"] == player]["x"],
            y = df[df["player"] == player]["y"]
        ),
        row = n // 2 + 1,
        col = n % 2 + 1
    )

fig.update_yaxes(
    visible = False
)

fig.update_xaxes(
    visible = False
)

fig.update_layout(
    height = 1500,
    title_text = "Desplaçaments dels personatges",
    paper_bgcolor = "rgba(256, 256, 256, 256)",
    plot_bgcolor = "rgba(230, 230, 230, 256)"
)

fig.show()
In [7]:
# OCUPACIÓ DE L'ESPAI

data = df[["x", "y"]].reset_index()
data["x"] = (data["x"] // .01).astype(int)
data["y"] = (data["y"] // .01).astype(int)

matrix = np.matrix([[data[(data["x"] == x) & (data["y"] == y)].shape[0] for x in range(100)] for y in range(100)])

fig = go.Figure(
    data = go.Heatmap(
        z = np.power(matrix, 1 / 3),
        x = list(range(100)),
        y = list(range(100))
    )
)

fig.update_yaxes(
    visible = False
)

fig.update_xaxes(
    visible = False
)

fig.update_layout(
    height = 600,
    width = 800,
    title_text = "Ocupació de l'espai",
    paper_bgcolor = "rgba(256, 256, 256, 256)",
    plot_bgcolor = "rgba(230, 230, 230, 256)"
)

fig.show()
In [8]:
# RITMES

ryt = df[["time", "distance"]].copy()
ryt = ryt.sort_values(by = "time").reset_index()
ryt = ryt.groupby("time").sum().reset_index()[["time", "distance"]]

fig = go.Figure(
    data = go.Scatter(
        mode = "lines",
        x = ryt["time"],
        y = ryt["distance"],
        fill = "tozeroy",
        fillcolor = "#333333",
        line = dict(
            width = .5,
            color = "#333333"
        )
    )
)

fig.update_yaxes(
    visible = False
)

fig.update_layout(
    height = 400,
    title_text = "Ritme de la obra"
)

fig.show()
In [9]:
fig = make_subplots(
    rows = 5,
    cols = 2,
    shared_yaxes = True
)

for n, player in enumerate(pd.unique(df["player"])):
    ryt = df[df["player"] == player][["time", "distance"]]
    ryt = ryt.sort_values(by = "time").reset_index()
    ryt = ryt.groupby("time").sum().reset_index()[["time", "distance"]]

    fig.add_trace(go.Scatter(
            name = player,
            mode = "lines",
            x = ryt["time"],
            y = ryt["distance"],
            fill = "tozeroy",
            line = dict(
                width = .5
            )
        ),
        row = n // 2 + 1,
        col = n % 2 + 1
    )
    
fig.update_yaxes(
    visible = False
)

fig.update_xaxes(
    visible = True
)

fig.update_layout(
    height = 1500,
    title_text = "Ritmes dels personatges",
    paper_bgcolor = "rgba(256, 256, 256, 256)",
    plot_bgcolor = "rgba(230, 230, 230, 256)"
)
    
fig.show()
In [12]:
players = list(np.unique(df["player"]))

data = df.copy()
data["time_bin"] = pd.cut(data["time"], 241)
data = data.groupby(["player", "time_bin"]).mean()

fig = go.Figure()

self = players.pop(0)

while self:
    [
        fig.add_trace(
            go.Box(
                name = self + " | " + him,
                x = 1 - (((data.loc[self]["x"] - data.loc[him]["x"]) ** 2) + (data.loc[self]["y"] - data.loc[him]["y"]) ** 2) ** (1 / 2.)/ math.sqrt((1 ** 2) * 2),
                boxpoints = 'all',
                jitter = 0.5,
                whiskerwidth = 0.2,
                marker_size = 2,
                line_width = 1
            )
        )
        for him in players
    ]
    if len(players):
        self = players.pop(0)
    else:
        self = None
        
fig.update_yaxes(
    visible = True
)

fig.update_xaxes(
    visible = True,
    title = "Proximitat sent 0 el mínim i 1 el màxim"
)
        
fig.update_layout(
    height = 1500,
    title_text = "Proximitat dels personatges sobre l'escenari",
    paper_bgcolor = "rgba(256, 256, 256, 256)",
    plot_bgcolor = "rgba(230, 230, 230, 256)",
    showlegend = False
)
    
fig.show()